package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.EduResBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.zzhou.common.library.util.ToastShow;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/20.
 */

public class EduResAdapter extends XRecyclerView.Adapter<EduResAdapter.ViewHolder> {
    private List<EduResBean> mlist;
    private Context mcontext;

    public EduResAdapter(Context context, List<EduResBean> datas) {
        this.mcontext = context;
        this.mlist = datas;
    }

    public void addAll(List<EduResBean> data) {
        mlist = data;
        notifyDataSetChanged();
    }

    public void addAllForSearch(List<EduResBean> data, boolean append) {
        if (mlist != null) {
            if (!append) {
                mlist.clear();
            }
            mlist.addAll(data);
        } else {
            mlist = data;
        }
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_edu_res, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        EduResBean info = mlist.get(position);
        ImageLoader.loadImg(mcontext, info.getResicon(), holder.item_edu_res_pic);
        holder.item_edu_res_name.setText(info.getResname().length() < 10 ? info.getResname() : info.getResname().substring(0, 9));
        holder.item_edu_res_year.setText("版本：" + info.getYear());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastShow.show(mcontext, "待添加");
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView item_edu_res_pic;
        TextView item_edu_res_name;
        TextView item_edu_res_year;

        public ViewHolder(View convertView) {
            super(convertView);
            item_edu_res_pic = convertView.findViewById(R.id.item_edu_res_pic);
            item_edu_res_name = convertView.findViewById(R.id.item_edu_res_name);
            item_edu_res_year = convertView.findViewById(R.id.item_edu_res_year);
        }
    }
}
